package com.hxd.qzg.common;

import java.util.regex.Pattern;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.NameFilter;
import com.alibaba.fastjson.serializer.SerializeFilter;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.ValueFilter;
import com.jfinal.core.Controller;
import com.jfinal.core.NotAction;
import com.jfinal.kit.Ret;

import cn.hutool.core.convert.Convert;

public abstract class BaseController extends Controller {


    private ValueFilter filter = new ValueFilter() {
        @Override
        public Object process(Object obj, String s, Object v) {
            if(v==null){
                return "";
            }
            return v;
        }
    };
    private NameFilter filter1 = new NameFilter() {
        @Override
        public String process(Object obj, String s, Object v) {
            return humpToLine2(s);
        }
    };

    private SerializeFilter[] filters = {filter1,filter};

    private static Pattern humpPattern = Pattern.compile("[A-Z]");


    @NotAction
    public Long getFlowId(){
        return Convert.toLong(getHeader("flowid"));
    }

    @NotAction
    public String getLoclhostPath() {
        return getRequest().getScheme() + "://" + getRequest().getServerName() + ":" + getRequest().getServerPort();
    }

    @NotAction
    protected String getContextPath() {
        return getRequest().getScheme() + "://" + getRequest().getServerName() + ":" + getRequest().getServerPort() + getRequest().getContextPath();
    }

  
    @NotAction
    public void renderJsonNotNull(Ret ret){
        renderJson(JSON.toJSONString(ret,filters,SerializerFeature.WriteDateUseDateFormat));
    }

    /** 驼峰转下划线,效率比上面高 */
    public static String humpToLine2(String str) {

        return str;
    }

}
